<template>
  <BasicModal v-bind="$attrs" :width="width" @register="register" :title="title" @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <LabItemsTemp2BasicForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="true"></LabItemsTemp2BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose } from 'vue';
  import LabItemsTemp2BasicForm from './LabBasicForm.vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';

  const title = ref<string>('基础信息');
  const width = ref<number>(600);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);

  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({ height: 120 });
    if (data.flag == '1') {
      add(data);
    } else {
      edit(data.record);
    }
  });

  /**
   * 新增
   */
  function add(data) {
    title.value = '新增-基础信息';
    visible.value = true;
    nextTick(() => {
      registerForm.value.add(data);
    });
  }

  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    title.value = disableSubmit.value ? '详情-基础信息' : '编辑-基础信息';
    visible.value = true;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    closeModal();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
